/*
 * KeyManager.java
 * Created on October 23, 2007, 12:04 PM
 */

package view;

import utils.CircularBinaryList;

/**
 *
 * @author Erol Hira
 */
public class KeyManager {
      
    private Component component;
    
    private boolean keyWritten;
    private boolean isSleep;
    private boolean notified;
        
    private CircularBinaryList cbl;    
    
    private int repeatCount;        
    
    private final int SLEEP_DURATION = 400;        
    
    
    public KeyManager(Component component) {
        
        this.component = component;
        
        keyWritten = true;
        
        cbl = new CircularBinaryList();
        
        t.start();
    }
    
    
    public final void keyPressed(int keyCode) {
        
        Container container = component.getContainer();
        
        cbl.pushFirst(keyCode);
        int gameCode = container.getGameAction(keyCode);        
        
        if(Container.UP == gameCode){
            
            component.doUP();
            
        } else if(Container.DOWN == gameCode){
            
            component.doDOWN();
            
        } else if(Container.RIGHT == gameCode){
            
            component.doRIGHT();
            
        } else if(Container.LEFT == gameCode){
            
            component.doLEFT();
            
        } else if(Container.FIRE == gameCode){
            
            component.doFire();
            
        } else {
            
            synchronized (t){
                notified = true;
                t.notify();
            }            
        }
    }            
    
    //---------------------------------------------------------------------
    
    private Thread t = new Thread(){            
        
        public void run(){
            
            while(true){
                                
                try {
                    
                    synchronized (t){
                        isSleep = true;
                        t.wait(SLEEP_DURATION);
                        
                        
                        if(notified == false){                                                        

                            // evaluate repeat and write
                            if(keyWritten == false){
                                 writeKey(cbl.getFirst());
                                 keyWritten = true;
                            }

                            cbl.reset();                           
                            
                            isSleep = false;
                            
                            t.wait();                            
                            
                        } else {
                            
                            isSleep = true;
                        }
                        
                        if(notified){
                            
                            keyWritten = false;
                            
                            if(isSleep){                                    
                                    if(cbl.getFirst() == cbl.getLast()){
                                    repeatCount++;
                                }else {
                                    writeKey(cbl.getLast());
                                    keyWritten = false;
                                }
                            }
                            
                            isSleep = false;
                            notified = false;
                        }
                        
                    }
                    
                } catch (InterruptedException ex) { }
            }
        }
    };
    
    public void writeKey(int keyCode){                        
        
        String key = getKey(keyCode);
        
        repeatCount = 0;
        
        component.doKey(key);
    }
    
    //---------------------------------------------------------------------
    
    
    private String[] KEY_NUM_2 = {"a","b","c"};
    private String[] KEY_NUM_3 = {"d","e","f"};
    private String[] KEY_NUM_4 = {"g","h","i"};
    private String[] KEY_NUM_5 = {"j","k","l"};
    private String[] KEY_NUM_6 = {"m","n","o"};
    private String[] KEY_NUM_7 = {"p","r","s"};
    private String[] KEY_NUM_8 = {"t","u","v"};
    private String[] KEY_NUM_9 = {"w","x","y","z"};
                        
    
    
    private String getKey(int keyCode){
        
        if(Container.KEY_NUM2 == keyCode){
            
            int index = repeatCount % KEY_NUM_2.length;
            return KEY_NUM_2[index];
            
        } else if(Container.KEY_NUM3 == keyCode){
            
            int index = repeatCount % KEY_NUM_2.length;
            return KEY_NUM_3[index];
            
        } else if(Container.KEY_NUM4 == keyCode){
            
            int index = repeatCount % KEY_NUM_2.length;
            return KEY_NUM_4[index];
            
        } else if(Container.KEY_NUM5 == keyCode){
            
            int index = repeatCount % KEY_NUM_2.length;
            return KEY_NUM_5[index];
            
        } else if(Container.KEY_NUM6 == keyCode){
            
            int index = repeatCount % KEY_NUM_2.length;
            return KEY_NUM_6[index];
            
        } else if(Container.KEY_NUM7 == keyCode){
            
            int index = repeatCount % KEY_NUM_2.length;
            return KEY_NUM_7[index];
            
        } else if(Container.KEY_NUM8 == keyCode){
            
            int index = repeatCount % KEY_NUM_2.length;
            return KEY_NUM_8[index];
            
        } else if(Container.KEY_NUM9 == keyCode){
            
            int index = repeatCount % KEY_NUM_2.length;
            return KEY_NUM_9[index];
            
        } else {
        
            return "";
        }
    }
}
